layui.use(['layer','jquery','form','table'],function () {
    let form = layui.form;
    let layer = layui.layer;
    let $ = layui.$;
    let table = layui.table;
    let user = JSON.parse(localStorage.getItem('user'));

    //返回功能
    $('#goBack').click(function () {
        parent.tabParentChange(3);
    })


    //为隐藏域赋值
    $('#userId').val(user.data.userId);
    //刷新功能
    $('#refresh').click(function () {
        location.reload();
    });

    //查询自己的全部课程
    getSelfCourses({userId: user.data.userId,display: 1});
    //根据课程价格、课程分类、课程名称查询自己的课程
    form.on('submit(search)',function (obj) {
        getSelfCourses(obj.field);
        return false;
    });
    //修改上架状态
    form.on('switch(courseStatus)',function (data) {
        if(data.elem.checked){
            updateCourseStatus(data.value,2);
        }else{
            updateCourseStatus(data.value,3);
        }
    });
    //高级检索
    $('#courseSearch').click(function () {
        layer.open({
            type : 2,
            title : "高级检索",
            area : [ '780px', '450px' ],
            shade : 0,
            offset: 'auto',
            content:'self-course-search.html',
        })
    });
    function updateCourseStatus(courseId,courseStatus) {
        $.ajax({
            url: '/course/updateCourseStatus',
            dataType: 'json',
            data:{
                courseStatus: courseStatus,
                courseId: courseId
            },
            type: 'post',
            success: function (data) {
                if(data.data){
                    if(courseStatus == 3){
                        layer.msg('下架成功')
                    }else if(courseStatus == 2){
                        layer.msg('上架成功')
                    }
                }
            }
        });
    }
    //根据课程名称、课程关键字、课程价格查询课程
    function getSelfCourses(course) {
        $.ajax({
            url: '/course/getSelfCourses',
            dataType: 'json',
            data: course,
            success: function (data) {
                selfCourseTableRender(data.data);
            }
        });
    }
    selfCourseTableRender = function selfCourseTableRender(course){
        table.render({
            elem: '#selfCourseTable',
            title: '个人课程列表',
            data: course,
            limit: 10,
            toolbar: '#course_thead',
            page:{
                layout:['count','prev','page','next','limit','skip','refresh'],
                //limits:[3,6,9],
                first:"首页",
                last:"尾页",
            },
            cols: [[
                {type:'checkbox'},
                {title:'序号',align:'center',templet:function(data){
                    return data.LAY_INDEX;
                }},
                {field:"",title:"课程图片",align:'center',templet:function(data){
                    return '<img src="'+ data.url +'" />';
                }},
                {field:"courseName",title:"课程名称",align:'center'},
                {field:"coursePrimary",title:"课程关键字",align:'center'},
                {field:"courseNprice",title:"课程现价",align:'center',templet:function(data){
                    return '￥' + data.courseNprice;
                }},
                {field:"",title:"审核状态",align:'center',templet:function(data){
                    if(data.result == 1){
                        return '审核通过';
                    }else if(data.result == 2){
                        return '待审核';
                    }else if(data.result == 3){
                        return '审核驳回';
                    }
                }},
                {field:"",title:"上架状态",align:'center',templet:function(data){
                    if(data.courseStatus == 2){
                        return '<input type="\checkbox\" value="'+data.courseId +'" lay-skin=\"switch\" lay-text=\"上架|下架"\ checked lay-filter="courseStatus" />';
                    }else if(data.courseStatus == 3){
                        return '<input type=\"checkbox\" value="'+data.courseId +'" lay-skin=\"switch\" lay-text=\"上架|下架"\ lay-filter="courseStatus">';
                    }
                }},
                {field:"className",title:"课程分类",align:'center'},
                {field:"saleNum",title:"课程销量",align:'center'},
                {field:"courseIntro",title:"课程简介",align:'center'},
                {field:'',title:"操作",align:'center',toolbar: '#operation'}
            ]]
        });
    }
    //表内工具栏点击
    table.on('tool(selfCourseTable)',function (obj) {
        //删除课程
        if(obj.event == 'deleteCourseTable'){
            var layerIndex = layer.confirm('确定要删除"' + obj.data.courseName + '"么?', {
                btn: ['确定','取消'],
                title: '删除课程分类',
            }, function(){
                deleteCourse(obj.data.courseId);
                layer.close(layerIndex);
            });
        }else if(obj.event == 'selectCourseTable'){
            layer.open({
                type: 2,
                title: '查看课程详情',
                content: 'course-details.html',
                area:["95%","99%"],
                scrollbar: false,
                offset:'auto',
                shade: 0.4,
                btn:['确定','取消'],
                yes:function(index,layero){
                    layer.close(index);
                },
                success:function (layero, index) {
                    var childHtml = layero.find('iframe').contents();
                    getCurrentCourse(childHtml,obj.data);
                }
            });
        }
    })
    function getCurrentCourse(childHtml, course) {
        $.ajax({
            url: '/course/getCourseByCourseId',
            dataType: 'json',
            data:{courseId: course.courseId,commentType: 2},
            success: function (data) {
                var newCourse = data.data;
                $.ajax({
                    url: '/log/addLog',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        tableName: 'js_course',
                        type: 5,
                        userId: user.data.userId,
                        remark: '查看个人课程详情信息',
                        logType: 2
                    },
                    success: function(data){
                        if(data.data.logId != null){
                            childHtml.find('#courseName').text(newCourse.courseName);
                            childHtml.find('#createDate').text(newCourse.createDate);
                            childHtml.find('#account').text(newCourse.account);
                            childHtml.find('#courseNprice').text(newCourse.courseNprice);
                            childHtml.find('#coursePrimary').text(newCourse.coursePrimary);
                            childHtml.find('#className').text(newCourse.className);
                            childHtml.find('#likeNum').text(newCourse.likeNum);
                            childHtml.find('#collectNum').text(newCourse.collectNum);
                            childHtml.find('#commentNum').text(newCourse.commentNum);
                            childHtml.find('#courseIntro').text(newCourse.courseIntro);
                            childHtml.find('#courseInfo').text(newCourse.courseInfo);
                            childHtml.find('#url').attr('src',newCourse.url);
                            childHtml.find('#courseUrl').attr('src',newCourse.courseUrl);
                            childHtml.find('#trueName').val(user.data.trueName);
                            childHtml.find('#courseId').val(course.courseId);
                            childHtml.find('.account').val(user.data.account);
                        }
                    }
                });

            }
        });
    }
    function deleteCourse(courseId) {
        $.ajax({
            url: '/course/deleteCourseByCourseId',
            type: 'post',
            dataType: 'json',
            data: {courseId: courseId,display: 2},
            success: function (data) {
                if(data.data){
                    $.ajax({
                        url: '/log/addLog',
                        type: 'post',
                        dataType: 'json',
                        data: {
                            tableName: 'js_course',
                            type: 3,
                            userId: user.data.userId,
                            remark: '删除个人课程',
                            logType: 2
                        },
                        success: function(data){
                            if(data.data.logId != null){
                                layer.msg('删除成功');
                                getSelfCourses({userId: user.data.userId,display: 1});
                            }
                        }
                    });
                }
            }
        });
    }
})
